<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Setting extends Access_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model("setting_model");
		$this->load->model("crud_model");
	}

	function index()
	{
		$page["_contents"] = "administrator/setting/v_setting";
		
		$row = $this->setting_model->getAll()->row();
		
		$data['logo'] = $row->logo; 

		$this->template->load("administrator/template/main_template",$page,$data);
	}
	
	function save()
	{
		
		$row = $this->setting_model->getAll()->row();

		if($_FILES['logo']['size'] == 0)
		{
			$data['logo'] = $row->logo;
			echo 'kosong';
			die();
		}
		else
		{
			$config['upload_path'] = './assets/honda_img';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']  = '1000';
			$config['max_width']  = '3000';
			$config['max_height']  = '3000';
			$config['file_name'] = 'Honda';

			$this->load->library('upload', $config);

			if ( ! $this->upload->do_upload('logo')){
				$error = array('error' => $this->upload->display_errors());
				$this->session->set_flashdata('error_alert', 'Kesalahan dalam mengunggah gambar. Gambar yang anda pilih melebihi ukuran maksimum yang diizinkan');
				redirect("administrator/setting");
			}
			else{
				unlink('./assets/honda_img/'.$row->logo);
				$upload = $this->upload->data();
				$data['logo'] = $upload['file_name'];
			}
		}
		
		$this->crud_model->update('setting', $row->id, $data, 'id');

		$this->session->set_flashdata('success_alert', 'Pengaturan telah disimpan');

		redirect("administrator/setting");
		


	}

	function load_log()
	{		
		$arr['data'] = array();
		$query = $this->setting_model->getLog();
		
		foreach($query->result() as $row)
		{
			$data = array();
			array_push($data,$row->username);
			array_push($data,$row->datetime);
			array_push($data,$row->ip);
			
			array_push($arr['data'],$data);
		}		
		echo json_encode($arr);
	}

}

/* End of file setting.php */
/* Location: ./application/controllers/administrator/setting.php */